package com.project.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.project.model.IndustryModel;
import com.project.model.IndustrySmallModel;
import com.project.service.IndustrySmallService;

@RestController
@RequestMapping(value = "industry")
public class IndustryController {
	
	@Autowired
	private IndustrySmallService industrySmallService;
	
	@RequestMapping("")
	public ModelAndView index(HttpServletRequest request) {
		ModelAndView view = new ModelAndView();
		// 获取推荐分类
		List<IndustrySmallModel> industrySmalls =  industrySmallService.listIndustrySmallModel();
		List<IndustryModel> industrys = new ArrayList<IndustryModel>();
		if (null != industrySmalls && industrySmalls.size() > 0) {
			for (int i=0; i<industrySmalls.size(); i++) {
				IndustryModel industry = new IndustryModel();
				industry.setId(industrySmalls.get(i).getParent_id());
				industry.setIcon(industrySmalls.get(i).getParent_icon());
				industry.setFont_color(industrySmalls.get(i).getParent_font_color());
				industry.setBackground_color(industrySmalls.get(i).getParent_background_color());
				industry.setName(industrySmalls.get(i).getParent_name());
				if (industrys.size() == 0) {
					industrys.add(industry);
				} else {
					boolean flag = true;
					for (int j = 0; j < industrys.size(); j++) {
						if (industry.getId() == industrys.get(j).getId()) {
							flag = false;
						}
					}
					if (flag) {
						industrys.add(industry);
					}
				}
			}
		} else {
			industrySmalls = new ArrayList<IndustrySmallModel>();
		}
		view.addObject("industrys", industrys);
		view.addObject("industrySmalls", industrySmalls);
		view.setViewName("industry.jsp");
		return view;
	}
}